from django.db import models
from django.utils.safestring import mark_safe
from mdeditor.fields import MDTextField


# 用户表
class User(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    name = models.CharField(verbose_name="昵称", max_length=22, default='')
    password = models.CharField(verbose_name="密码", max_length=32, default='')
    phone = models.CharField(verbose_name="手机号", max_length=11, default='')
    email = models.CharField(verbose_name="邮箱", max_length=22, default='')
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    photo = models.FileField(verbose_name="头像", default='', upload_to="user/photo/")
    is_active = models.BooleanField(verbose_name="活跃状态", default=True)

    def admin_sample(self):
        return mark_safe('<img src="/media/%s" height="60" width="60" />' % (self.photo,))

    admin_sample.short_description = '用户头像'
    admin_sample.allow_tags = True

    def __str__(self):
        return self.name

    class Meta:
        # 数据库列表名
        db_table = 'User'
        # 后台管理名
        verbose_name_plural = '用户管理'


# 新闻管理
class News(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    title = models.CharField(verbose_name="题目", max_length=128, default='')
    type = models.ForeignKey(verbose_name="类", to='Type', on_delete=models.CASCADE)
    photo = models.FileField(verbose_name="图片", default='', upload_to="news/photo/")
    content = MDTextField(blank=True, null=True, verbose_name='文章正文')
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    traffic = models.IntegerField(verbose_name="访问数量", default=0)
    is_active = models.BooleanField(verbose_name="活跃状态", default=True)

    def admin_sample(self):
        return mark_safe('<img src="/media/%s" height="90" width="90" />' % (self.photo,))

    admin_sample.short_description = '图片'
    admin_sample.allow_tags = True

    def __str__(self):
        return self.title

    def viewed(self):
        """
        增加阅读数
        :return:
        """
        self.traffic += 1
        self.save(update_fields=['traffic'])

    class Meta:
        # 数据库列表名
        db_table = 'news'
        # 后台管理名
        verbose_name_plural = '新闻管理'


# 留言管理
class Message(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    user = models.ForeignKey(verbose_name="用户", to='User', on_delete=models.CASCADE)
    news = models.ForeignKey(verbose_name="新闻", to='News', on_delete=models.CASCADE, null=True)
    message = models.TextField(verbose_name="留言", default='')
    is_active = models.BooleanField(verbose_name="是否活跃", default=True)
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    class Meta:
        # 数据库列表名
        db_table = 'message'
        # 后台管理名
        verbose_name_plural = '留言管理'

    def admin_sample(self):
        return mark_safe('<img src="/media/%s" height="60" width="60" />' % (self.user.photo,))

    admin_sample.short_description = '商品图片'
    admin_sample.allow_tags = True


# 新闻分类
class Type(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    type = models.CharField(verbose_name='类型', max_length=24, default='')
    is_active = models.BooleanField(verbose_name="是否活跃", default=True)
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    class Meta:
        # 数据库列表名
        db_table = 'Type'
        # 后台管理名
        verbose_name_plural = '新闻分类'

    def __str__(self):
        return self.type


# 友情链接管理
class Href(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    title = models.CharField(verbose_name="地址名", max_length=64, default='')
    href = models.CharField(verbose_name="网站地址", max_length=64, default='')
    photo = models.FileField(verbose_name="图片", default='', upload_to="Href/photo/")
    is_active = models.BooleanField(verbose_name="是否活跃", default=True)
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    class Meta:
        # 数据库列表名
        db_table = 'Href'
        # 后台管理名
        verbose_name_plural = '友情链接管理'

    def admin_sample(self):
        return mark_safe('<img src="/media/%s" height="90" width="90" />' % (self.photo,))

    admin_sample.short_description = '图片'
    admin_sample.allow_tags = True


# 赞赏管理
class Admiration(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    title = models.CharField(verbose_name="内容", max_length=64, default='')
    photo = models.FileField(verbose_name="图片", default='', upload_to="Admiration/photo/")
    is_active = models.BooleanField(verbose_name="是否活跃", default=True)
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    class Meta:
        # 数据库列表名
        db_table = 'Admiration'
        # 后台管理名
        verbose_name_plural = '赞赏'

    def admin_sample(self):
        return mark_safe('<img src="/media/%s" height="90" width="90" />' % (self.photo,))

    admin_sample.short_description = '图片'
    admin_sample.allow_tags = True


# 相关管理
class MyHref1(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    title = models.CharField(verbose_name="内容", max_length=64, default='')
    photo = models.FileField(verbose_name="图片", default='', upload_to="MyHref/photo/")
    is_active = models.BooleanField(verbose_name="是否活跃", default=True)
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    class Meta:
        # 数据库列表名
        db_table = 'MyHref'
        # 后台管理名
        verbose_name_plural = '相关链接管理'

    def admin_sample(self):
        return mark_safe('<img src="/media/%s" height="90" width="90" />' % (self.photo,))

    admin_sample.short_description = '图片'
    admin_sample.allow_tags = True


def __str__(self):
    return self.admin_sample
